| Conditions | 1 |
| Paths | 16 |
| Total Lines | 164 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { |
||
| 24 | export const getAsyncData = ({ |
||
| 25 | stateKey, dataSource, type, showTreeRootNode, extraParams = {} |
||
| 26 | }) => { |
||
| 27 | |||
| 28 | return (dispatch) => { |
||
| 29 | |||
| 30 | dispatch(dismissEditor({ stateKey })); |
||
| 31 | |||
| 32 | dispatch( |
||
| 33 | setLoaderState({state: true, stateKey }) |
||
| 34 | ); |
||
| 35 | |||
| 36 | if (typeof dataSource === 'function') { |
||
| 37 | |||
| 38 | // passing extraParams.parentId |
||
| 39 | // to custom func so they can do partial |
||
| 40 | // loading |
||
| 41 | dataSource(extraParams).then((response) => { |
||
| 42 | |||
| 43 | if (response && response.data) { |
||
| 44 | |||
| 45 | dispatch( |
||
| 46 | setLoaderState({ state: false, stateKey }) |
||
| 47 | ); |
||
| 48 | |||
| 49 | if (type !== 'tree') { |
||
| 50 | |||
| 51 | dispatch({ |
||
| 52 | type: SET_DATA, |
||
| 53 | data: response.data, |
||
| 54 | total: response.total, |
||
| 55 | currentRecords: response.data, |
||
| 56 | success: true, |
||
| 57 | stateKey, |
||
| 58 | editMode: extraParams.editMode |
||
| 59 | }); |
||
| 60 | } |
||
| 61 | |||
| 62 | else { |
||
| 63 | // upon the return of read |
||
| 64 | // response needs to clarify |
||
| 65 | // whether this is a partial update |
||
| 66 | dispatch(setTreeData({ |
||
| 67 | data: response.data, |
||
| 68 | stateKey, |
||
| 69 | showTreeRootNode, |
||
| 70 | parentId: extraParams.parentId, |
||
| 71 | partial: response.partial, |
||
| 72 | editMode: extraParams.editMode |
||
| 73 | })); |
||
| 74 | } |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (response && !response.data) { |
||
| 80 | /* eslint-disable no-console */ |
||
| 81 | console.warn( |
||
| 82 | `A response was recieved |
||
| 83 | but no data entry was found` |
||
| 84 | ); |
||
| 85 | console.warn( |
||
| 86 | `Please see |
||
| 87 | https://github.com/bencripps/react-redux-grid |
||
| 88 | for documentation` |
||
| 89 | ); |
||
| 90 | /* eslint-enable no-console */ |
||
| 91 | } |
||
| 92 | |||
| 93 | dispatch( |
||
| 94 | setLoaderState({ state: false, stateKey }) |
||
| 95 | ); |
||
| 96 | |||
| 97 | dispatch({ |
||
| 98 | type: ERROR_OCCURRED, |
||
| 99 | error: 'Unable to Retrieve Grid Data', |
||
| 100 | errorOccurred: true, |
||
| 101 | stateKey |
||
| 102 | }); |
||
| 103 | |||
| 104 | }); |
||
|
|
|||
| 105 | } |
||
| 106 | |||
| 107 | else if (typeof dataSource === 'string') { |
||
| 108 | |||
| 109 | if (type !== 'tree') { |
||
| 110 | |||
| 111 | return Request.api({ |
||
| 112 | route: dataSource, |
||
| 113 | method: 'GET' |
||
| 114 | }).then((response) => { |
||
| 115 | |||
| 116 | if (response && response.data) { |
||
| 117 | |||
| 118 | dispatch({ |
||
| 119 | type: SET_DATA, |
||
| 120 | data: response.data, |
||
| 121 | total: response.total, |
||
| 122 | currentRecords: response.data, |
||
| 123 | success: true, |
||
| 124 | stateKey, |
||
| 125 | editMode: extraParams.editMode |
||
| 126 | }); |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | else { |
||
| 131 | dispatch({ |
||
| 132 | type: ERROR_OCCURRED, |
||
| 133 | error: 'Unable to Retrieve Grid Data', |
||
| 134 | errorOccurred: true, |
||
| 135 | stateKey |
||
| 136 | }); |
||
| 137 | } |
||
| 138 | |||
| 139 | dispatch( |
||
| 140 | setLoaderState({state: false, stateKey }) |
||
| 141 | ); |
||
| 142 | }); |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 146 | return Request.api({ |
||
| 147 | route: dataSource, |
||
| 148 | method: 'GET', |
||
| 149 | queryStringParams: { |
||
| 150 | parentId: extraParams.parentId |
||
| 151 | } |
||
| 152 | }).then((response) => { |
||
| 153 | |||
| 154 | if (response && response.data) { |
||
| 155 | |||
| 156 | // response needs to specify |
||
| 157 | // whether this is full or partial update |
||
| 158 | dispatch(setTreeData({ |
||
| 159 | data: response.data, |
||
| 160 | stateKey, |
||
| 161 | showTreeRootNode, |
||
| 162 | partial: response.partial, |
||
| 163 | parentId: extraParams.parentId |
||
| 164 | })); |
||
| 165 | |||
| 166 | } |
||
| 167 | |||
| 168 | else { |
||
| 169 | dispatch({ |
||
| 170 | type: ERROR_OCCURRED, |
||
| 171 | error: 'Unable to Retrieve Grid Data', |
||
| 172 | errorOccurred: true, |
||
| 173 | stateKey |
||
| 174 | }); |
||
| 175 | } |
||
| 176 | |||
| 177 | dispatch( |
||
| 178 | setLoaderState({state: false, stateKey }) |
||
| 179 | ); |
||
| 180 | }); |
||
| 181 | |||
| 182 | } |
||
| 183 | |||
| 184 | }; |
||
| 185 | }; |
||
| 186 | |||
| 187 | export const setColumns = ({ columns, stateKey, stateful }) => { |
||
| 188 | |||
| 428 |